//
//  ZYCTeaLeaveApproveView.m
//  ZDSC
//
//  Created by 张威 on 2021/10/26.
//

#import "ZYCTeaLeaveApproveView.h"

#define BTProblemTotalLenght 50

@interface ZYCTeaLeaveApproveView ()<UITextViewDelegate>

@property (nonatomic, strong) UIView *baseView;
@property (nonatomic, strong) UILabel *tipLabel;
@property (nonatomic, strong) UIView *line;
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UILabel *numLabel;
@property (nonatomic, strong) UILabel *placeHoldLabel;
@property (nonatomic, strong) UIButton *comfirmButton;


@end
@implementation ZYCTeaLeaveApproveView

- (void)setType:(ZYCTeaLeaveApproveType)type{
    _type = type;
    if (type == ZYCTeaLeaveApproveTypeRevoke) {
        self.tipLabel.text = @"审核已通过的请假，如要撤销需要提交撤销申请";
        self.placeHoldLabel.text = @"请输入撤销理由(非必填)";
        [self.comfirmButton setTitle:@"确认撤销" forState:UIControlStateNormal];
        NSArray *colorArray = @[(__bridge id)HEX_RGB(@"#F6EEB0").CGColor,
                                (__bridge id)HEX_RGB(@"#009B4C").CGColor];
        CAGradientLayer *gradientLayer = [self addGradientLayerWithColorArray:colorArray];
        [self.comfirmButton.layer insertSublayer:gradientLayer atIndex:0];
    }else{
        self.tipLabel.text = @"填写拒绝理由";
        self.placeHoldLabel.text = @"请输入(非必填)";
        [self.comfirmButton setTitle:@"确认拒绝申请" forState:UIControlStateNormal];
        [self.comfirmButton setBackgroundColor:HEX_RGB(@"#E93C06")];
    }
}
- (instancetype)initWithFrame:(CGRect)frame type:(ZYCTeaLeaveApproveType)type{
    self = [super initWithFrame:frame];
    if (self) {
        self.type = type;
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = HEX_RGB(@"#FAFAFA");
        [self addSubview:self.baseView];
        [self addSubview:self.comfirmButton];
        
        [self.baseView addSubview:self.tipLabel];
        [self.baseView addSubview:self.line];
        [self.baseView addSubview:self.textView];
        [self.baseView addSubview:self.placeHoldLabel];
        [self.baseView addSubview:self.numLabel];
        
        [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(10.0f);
            make.right.equalTo(self).offset(-10.0f);
            make.top.equalTo(self).offset(12.0f);
            make.height.mas_equalTo(222.0f);
        }];
        
        [self.comfirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(66.0f);
            make.right.equalTo(self).offset(-66.0f);
            make.top.equalTo(self.baseView.mas_bottom).offset(36.0f);
            make.height.mas_equalTo(40.0f);
        }];
        
        [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.baseView).offset(10.0f);
            make.right.equalTo(self.baseView).offset(-10.0f);
            make.top.equalTo(self.baseView).offset(15.0f);
        }];
        
        [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.baseView).offset(10.0f);
            make.right.equalTo(self.baseView).offset(-10.0f);
            make.top.equalTo(self.tipLabel.mas_bottom).offset(15.0f);
            make.height.mas_equalTo(1.0f);
        }];
        
        [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.baseView).offset(10.0f);
            make.right.equalTo(self.baseView).offset(-10.0f);
            make.top.equalTo(self.line.mas_bottom).offset(15.0f);
            make.bottom.equalTo(self.baseView).offset(-15.0f);
        }];
        
        [self.placeHoldLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(self.textView);
        }];

        [self.numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.right.equalTo(self.textView);
        }];
        
    }
    return self;
}

#pragma mark UITextViewDelegate
-(void) textViewDidEndEditing:(UITextView *)textView{
    if (textView.text.length < 1) {
        self.placeHoldLabel.hidden = NO;
    }else{
        self.placeHoldLabel.hidden = YES;
    }
}

-(void) textViewDidChange:(UITextView *)textView{
    self.numLabel.text = [NSString stringWithFormat:@"%ld/%d", textView.text.length, BTProblemTotalLenght];
}

-(BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if (textView.text.length >= BTProblemTotalLenght && text.length > 0) {
        return NO;
    }
    return YES;
}

-(void) textViewDidBeginEditing:(UITextView *)textView
{
    self.placeHoldLabel.hidden = YES;
}


#pragma mark - getters
- (NSString *)cancleReason{
    if (self.textView.text.length > 0) {
        return self.textView.text;
    }
    return @"";
}
- (UITextView *)textView {
    if (!_textView) {
        _textView = [[UITextView alloc] init];
        _textView.delegate = self;
        _textView.font = kCustomFont(@"PingFangSC-Light", 15.0f);
        _textView.textColor = HEX_RGB(@"333333");
    }
    return _textView;
}

- (UILabel *)tipLabel {
    if (!_tipLabel) {
        _tipLabel = [UILabel LabelWithFont:kCustomFont(@"PingFangSC-Light", 17.0f) titleColor:HEX_RGB(@"333333") textAlignment:ZZTextAlignmentLeft];
        _tipLabel.numberOfLines = 0;
    }
    return _tipLabel;
}
- (UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = HEX_RGB(@"#EDEDED");
    }
    return _line;
}

- (UILabel *)placeHoldLabel {
    if (!_placeHoldLabel) {
        _placeHoldLabel = [UILabel LabelWithFont:kCustomFont(@"PingFangSC-Light", 15.0f) titleColor:HEX_RGB(@"#D1D1D1") textAlignment:ZZTextAlignmentLeft];
    }
    return _placeHoldLabel;
}

- (UILabel *)numLabel {
    if (!_numLabel) {
        _numLabel = [UILabel LabelWithString:@"0/50" Font:kCustomFont(@"PingFangSC-Light", 15.0f) titleColor:HEX_RGB(@"#999999") textAlignment:ZZTextAlignmentLeft];
    }
    return _numLabel;
}

- (UIView *)baseView {
    if (!_baseView) {
        _baseView = [[UIView alloc] init];
        _baseView.backgroundColor = HEX_RGB(@"FFFFFF");
        _baseView.layer.cornerRadius = 7.5f;
        _baseView.layer.masksToBounds = YES;
    }
    return _baseView;
}

- (UIButton *)comfirmButton {
    if (!_comfirmButton) {
        _comfirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _comfirmButton.frame = CGRectMake(0,0, kScreenWidth - 2 * 66, 40);

        [_comfirmButton setTitleColor:HEX_RGB(kTitleWhiteColor) forState:UIControlStateNormal];
//        _comfirmButton.backgroundColor = HEX_RGB(@"#41B166");
        _comfirmButton.titleLabel.font = kSystemBoldFont(18.0f);
        [_comfirmButton addTarget:self action:@selector(clickComfirmAction:) forControlEvents:UIControlEventTouchUpInside];
        _comfirmButton.layer.cornerRadius = 20;
        _comfirmButton.layer.masksToBounds = YES;
    }
    return _comfirmButton;
}


- (CAGradientLayer *)addGradientLayerWithColorArray:(NSArray *)colorArray{
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1, 0);
    gradientLayer.colors = colorArray;
    gradientLayer.locations = @[@(0.3),@(1.0f)];
    gradientLayer.frame = self.comfirmButton.bounds;
    gradientLayer.cornerRadius = 20;
    return gradientLayer;
}

- (void)clickComfirmAction:(UIButton *)sender{
    if ([self.delegate respondsToSelector:@selector(teaLeaveApproveViewWithType:)]) {
        [self.delegate teaLeaveApproveViewWithType:self.type];
    }
}
@end
